package lenovo.asd.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class WifiXMLContentHandler extends DefaultHandler {
	
	private List<WifiTask> wifiTasks;
	private WifiTask wifiTask;
	private String preTag;
	
	public List<WifiTask> getWifiTasks() {
		return wifiTasks;
	}
 	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (wifiTask != null) {
			String data = new String (ch, start, length);
			// me: change element name with config.xml
			if ("ssid".equals(preTag)) {
				wifiTask.setSsid(data);
			} else if ("mac".equals(preTag)) {
				wifiTask.setMac(data);
			} else if ("usrname".equals(preTag)) {
				wifiTask.setUsrname(data);
			} else if ("passwd".equals(preTag)) {
				wifiTask.setPasswd(data);
			} else if ("url".equals(preTag)) {
				wifiTask.setUrl(data);
			} else if ("loop".equals(preTag)) {
				wifiTask.setLoop(new Short(data));
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if ("wifi".equals(localName) && wifiTask!=null) {
			wifiTasks.add(wifiTask);
			wifiTask = null;
		}
		preTag = null;
	}

	@Override
	public void startDocument() throws SAXException {
		wifiTasks = new ArrayList<WifiTask>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if ("wifi".equals(localName)) {
			wifiTask = new WifiTask();
			wifiTask.setId(new Integer(attributes.getValue("", "id")));
		}
		preTag = localName;
	}
	
	

}
